package com.jozen.smart_toilet_system.web_socket;


import android.text.TextUtils;


import com.jozen.modelbase.netwoek.util.JsonUtil;
import com.jozen.smart_toilet_system.BaseApplication;
import com.jozen.smart_toilet_system.R;
import com.zhangke.websocket.SimpleDispatcher;
import com.zhangke.websocket.dispatcher.ResponseDelivery;
import com.zhangke.websocket.response.ErrorResponse;
import com.zhangke.websocket.response.Response;
import com.zhangke.websocket.response.ResponseFactory;

import java.nio.ByteBuffer;

/**
 * @Description: 消息分发器
 */
public class AppResponseDispatcher extends SimpleDispatcher {

    /**
     * JSON 数据格式错误
     */
    public static final int JSON_ERROR = 11;
    /**
     * code 码错误
     */
    public static final int CODE_ERROR = 12;

    @Override
    public void onMessage(String message, ResponseDelivery delivery) {
        try {
            BaseCommDataModel response = JsonUtil.getBeanFromJson(message, BaseCommDataModel.class);
            if (response != null) {
                if (!TextUtils.isEmpty(response.getFunc())) {
                    delivery.onMessage(message, response.getFunc());
                } else {
                    ErrorResponse errorResponse = ResponseFactory.createErrorResponse();
                    errorResponse.setErrorCode(CODE_ERROR);
                    Response<String> textResponse = ResponseFactory.createTextResponse();
                    textResponse.setResponseData(message);
                    errorResponse.setResponseData(textResponse);
                    errorResponse.setReserved(response);
                    onSendDataError(errorResponse, delivery);
                }
            }

        } catch (Exception e) {
            ErrorResponse errorResponse = ResponseFactory.createErrorResponse();
            Response<String> textResponse = ResponseFactory.createTextResponse();
            textResponse.setResponseData(message);
            errorResponse.setResponseData(textResponse);
            errorResponse.setErrorCode(JSON_ERROR);
            errorResponse.setCause(e);
            onSendDataError(errorResponse, delivery);
        }
    }

    @Override
    public void onMessage(ByteBuffer byteBuffer, ResponseDelivery delivery) {
        super.onMessage(byteBuffer, delivery);
    }

    /**
     * 统一处理错误信息，
     * 界面上可使用 ErrorResponse#getDescription() 来当做提示语
     */
    @Override
    public void onSendDataError(ErrorResponse error, ResponseDelivery delivery) {
        try{
            switch (error.getErrorCode()) {
                case ErrorResponse.ERROR_NO_CONNECT:
                    error.setDescription(BaseApplication.getInstance().getApplicationContext().getResources().getString(R.string.net_work_error_txt));
                    break;
                case ErrorResponse.ERROR_UN_INIT:
                    error.setDescription(BaseApplication.getInstance().getApplicationContext().getResources().getString(R.string.connection_not_initialized));
                    break;
                case ErrorResponse.ERROR_UNKNOWN:
                    error.setDescription(BaseApplication.getInstance().getApplicationContext().getResources().getString(R.string.unknown_error_txt));
                    break;
                case JSON_ERROR:
                    error.setDescription(BaseApplication.getInstance().getApplicationContext().getResources().getString(R.string.data_format_exception));
                    break;
                case CODE_ERROR:
                    error.setDescription(BaseApplication.getInstance().getApplicationContext().getResources().getString(R.string.response_code_error));
                    break;
            }
            delivery.onSendDataError(error);
        }catch (Exception e){

        }

    }
}
